ABC253 C - Max - Min Query
提出
code: python
from collections import defaultdict
q = int(input())
d = defaultdict(int)
minmax = set()
for query in queries:
res = min(d[query1], query2) else:
print(max(minmax) - min(minmax))
解答
code: python
from collections import defaultdict
import heapq
q = int(input())
d = defaultdict(int)
maxq = []
minq = []
for query in queries:
if n == 1:
_, x = query
heapq.heappush(minq, x)
# heapqは最小値しか取り出せないから-を掛ける
heapq.heappush(maxq, -x)
elif n == 2:
_, x, c = query
else:
qmin = heapq.heappop(minq)
# 次の最小値を取り出す
qmin = heapq.heappop(minq)
qmax = heapq.heappop(maxq)
qmax *= -1
# 次の最大値を取り出す
qmax = heapq.heappop(maxq)
qmax *= -1
print(qmax - qmin)
# 最小値、最大値をheapq へ戻す
heapq.heappush(minq, qmin)
heapq.heappush(maxq, -qmax)
テーマ
メモ
提出
TLE
code: python
from collections import defaultdict
q = int(input())
d = defaultdict(int)
# max と min の管理
# 0 になったとき
for query in queries:
if n == 1:
_, x = query
# if x > maxn:
# maxn = x
# if x < minn:
# minn = x
elif n == 2:
_, x, c = query
else:
maxn = -1
minn = 1e9
for k, v in d.items():
if v == 0:
continue
if k > maxn:
maxn = k
if k < minn:
minn = k
print(maxn - minn)